package com.yida.gwj.expend.outside.config;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.domain.R;
import com.yida.common.core.exception.CheckedException;
import com.yida.common.redis.service.RedisService;
import com.yida.gwj.api.domain.GwjConfBusinessFlag;
import com.yida.gwj.api.domain.GwjConfStep;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.expend.outside.enums.BusinessBaseTypeEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 启动初始化数据
 * <p>
 * 1.gwj_conf_flow 2.gwj_conf_step
 *
 * @author liangp
 * @date 2023/04/20
 */
@Configuration
public class ExpendOutsideInit {

    private final Logger log = LoggerFactory.getLogger(ExpendOutsideInit.class);

    private final String cache = "gwj:expend:outside:infoInit:";
    @Autowired
    private RedisService redisService;
    @Autowired
    private RemoteGwjConfigService remoteGwjConfigService;

    @Async
    @Order
    @EventListener({WebServerInitializedEvent.class})
    public void init() {
        // log.info("[公维金资金申请][外网]初始化数据......");
        if (log.isDebugEnabled()) {
            log.debug("初始化");
        }
        try {
            R<List<GwjConfBusinessFlag>> flagR = remoteGwjConfigService.selectConfBusinessFlagList(new GwjConfBusinessFlag(), SecurityConstants.INNER);
            if (R.isError(flagR)) {
                // log.error("远程调用获取配置失败 --> [{}]", flagR.getMsg());
                throw new CheckedException("远程调用获取配置失败 --> code:" + flagR.getCode() + ";msg:" + flagR.getMsg());
            }
            if (ObjectUtil.isNull(flagR.getData())) {
                log.error("远程调用获取配置为空");
            }
            redisService.setCacheObject(cache + BusinessBaseTypeEnum.FLAG, flagR.getData());
            log.info("selectConfBusinessFlagList列表 --> [{}]", flagR.getData().toString());
        } catch (Exception e) {
            log.error("远程调用 -> [remoteGwjConfigService][selectConfBusinessFlagList]异常,异常信息: [{}]", e);
        }
        try {
            R<List<GwjConfStep>> stepR = remoteGwjConfigService.selectConfStepList(new GwjConfStep(), SecurityConstants.INNER);
            if (R.isError(stepR)) {
                // log.error("远程调用获取配置失败 --> [{}]", stepR.getMsg());
                throw new CheckedException("远程调用获取配置失败 --> code:" + stepR.getCode() + ";msg:" + stepR.getMsg());
            }
            if (ObjectUtil.isNull(stepR.getData())) {
                log.error("远程调用获取配置为空");
            }
            redisService.setCacheObject(cache + BusinessBaseTypeEnum.STEP, stepR.getData());
            log.info("selectConfStepList列表 --> [{}]", stepR.getData().toString());
        } catch (Exception e) {
            log.error("远程调用 -> [remoteGwjConfigService][selectConfStepList]异常,异常信息: [{}]", e);
        }

    }

}
